package top.yokey.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import top.yokey.android.R;
import top.yokey.android.base.BaseActivity;
import top.yokey.android.http.BaseCallBack;
import top.yokey.android.http.HttpHelp;

public class RegisterActivity extends BaseActivity {

    private AppCompatImageView closeImageView;
    private AppCompatEditText mobileEditText;
    private AppCompatEditText passwordEditText;
    private AppCompatEditText nicknameEditText;
    private AppCompatTextView registerTextView;
    private View mobileView, passwordView, nicknameView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register);
        super.onCreate(savedInstanceState);
        String data = getIntent().getStringExtra("mobile");
        mobileEditText.setText(data);
    }


    @Override
    public void initView() {

        closeImageView = findViewById(R.id.closeImageView);
        mobileEditText = findViewById(R.id.mobileEditText);
        passwordEditText = findViewById(R.id.passwordEditText);
        nicknameEditText = findViewById(R.id.nicknameEditText);
        registerTextView = findViewById(R.id.registerTextView);
        mobileView = findViewById(R.id.mobileView);
        passwordView = findViewById(R.id.passwordView);
        nicknameView = findViewById(R.id.nicknameView);

    }

    @Override
    public void initEvent() {

        //编辑框的焦点事件
        mobileEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                mobileView.setBackgroundResource(R.color.primary);
                passwordView.setBackgroundResource(R.color.textHint);
                nicknameView.setBackgroundResource(R.color.textHint);
            }
        });
        passwordEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                mobileView.setBackgroundResource(R.color.textHint);
                passwordView.setBackgroundResource(R.color.primary);
                nicknameView.setBackgroundResource(R.color.textHint);
            }
        });
        nicknameEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                mobileView.setBackgroundResource(R.color.textHint);
                passwordView.setBackgroundResource(R.color.textHint);
                nicknameView.setBackgroundResource(R.color.primary);
            }
        });

        //单击事件
        closeImageView.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.putExtra("username", "123456789");
            setResult(1, intent);
            finish();
        });

        registerTextView.setOnClickListener(view -> {
            String mobile = mobileEditText.getText().toString();
            String password = passwordEditText.getText().toString();
            String nickname = nicknameEditText.getText().toString();
            if (mobile.isEmpty() || password.isEmpty() || nickname.isEmpty()) {
                Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
                return;
            }
            //禁用注册按钮，防止用户一直点点点
            registerTextView.setEnabled(false);
            registerTextView.setText("正在注册...");
            //使用HttpHelp发送请求
            HttpHelp.get().ready("base", "register")
                    .add("mobile", mobile)
                    .add("password", password)
                    .add("nickname", nickname)
                    .get(new BaseCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }

                        @Override
                        public void onError(String reason) {
                            Toast.makeText(RegisterActivity.this, reason, Toast.LENGTH_SHORT).show();
                            registerTextView.setEnabled(true);
                            registerTextView.setText("注册");
                        }
                    });
        });

    }

}
